package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.IAppUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppTradeRecord;
import com.ruoyi.system.service.IAppTradeRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流水记录Controller
 *
 * @author ruoyi
 * @date 2024-08-12
 */
@RestController
@RequestMapping("/system/userRecord")
public class AppTradeRecordController extends BaseController {
    @Autowired
    private IAppTradeRecordService appTradeRecordService;
    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppTradeRecord appTradeRecord) {
        startPage();
        appTradeRecord.setType(0);
        List<AppTradeRecord> list = appTradeRecordService.selectAppTradeRecordList(appTradeRecord);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:userRecord:list2')")
    @GetMapping("/list2")
    public TableDataInfo list2(AppTradeRecord appTradeRecord) {
        startPage();
        appTradeRecord.setType(1);
        List<AppTradeRecord> list = appTradeRecordService.selectAppTradeRecordList(appTradeRecord);
        return getDataTable(list);
    }

    /**
     * 导出流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:export')")
    @Log(title = "流水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppTradeRecord appTradeRecord) {
        List<AppTradeRecord> list = appTradeRecordService.selectAppTradeRecordList(appTradeRecord);
        ExcelUtil<AppTradeRecord> util = new ExcelUtil<AppTradeRecord>(AppTradeRecord.class);
        util.exportExcel(response, list, "流水记录数据");
    }

    /**
     * 获取流水记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(appTradeRecordService.selectAppTradeRecordById(id));
    }

    /**
     * 新增流水记录
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:add')")
    @Log(title = "流水记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppTradeRecord appTradeRecord) {
        return toAjax(appTradeRecordService.insertAppTradeRecord(appTradeRecord));
    }

    /**
     * 修改流水记录 - 提现
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:edit')")
    @Log(title = "流水记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppTradeRecord appTradeRecord) {
        appTradeRecord.setAuditTime(new Date());
        // 审核状态，0：未提交，1：待确认，2：已确认，3：未通过
        if (appTradeRecord.getAuditType() == 2) {
            // 进入进入用户信息
            appUserService.changeUserBalance(appTradeRecord.getMoney(), appTradeRecord.getUserId(), "add", "充值");
        }
        return toAjax(appTradeRecordService.updateAppTradeRecord(appTradeRecord));
    }

    /**
     * 修改流水记录 - 充值
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:edit')")
    @Log(title = "流水记录", businessType = BusinessType.UPDATE)
    @PutMapping("/edit2")
    public AjaxResult edit2(@RequestBody AppTradeRecord appTradeRecord) {
        appTradeRecord.setAuditTime(new Date());
        // 审核状态，0：未提交，1：待确认，2：已确认，3：未通过
        if (appTradeRecord.getAuditType() == 3) {
            // 进入进入用户信息
            appUserService.changeUserBalance(appTradeRecord.getMoney(), appTradeRecord.getUserId(), "add", "提现失败返还余额");
        }
        return toAjax(appTradeRecordService.updateAppTradeRecord(appTradeRecord));
    }

    /**
     * 删除流水记录
     */
    @PreAuthorize("@ss.hasPermi('system:userRecord:remove')")
    @Log(title = "流水记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(appTradeRecordService.deleteAppTradeRecordByIds(ids));
    }
}
